/***************************************************************************
 *   Copyright (C) 2009 by Bernhard Neuhofer   *
 *   mail@bneu.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "../AS/corbaAddressServer.h"
#include "CClientSM.h"
#include <iostream>
#include <assert.h>


using namespace std;

CClientSM::CClientSM()
{
    try
    {
        int argc=0;
        char** argv=0;
        CORBA::ORB_var orb =CORBA::ORB_init ( argc,argv );

        CORBA::Object_var obj=orb->resolve_initial_references ( "NameService" );

        assert ( !CORBA::is_nil ( obj.in() ) );

        CosNaming::NamingContext_var nc=CosNaming::NamingContext::_narrow ( obj.in() );
        assert ( !CORBA::is_nil ( nc.in() ) );

        CosNaming::Name _corbaCosName;
        _corbaCosName.length ( 1 );
        _corbaCosName[0].id=CORBA::string_dup ( "SM" );

        CORBA::Object_var obj1=nc->resolve ( _corbaCosName );
        assert ( !CORBA::is_nil ( obj1.in() ) );

        m_sm=AddressServer::sm::_narrow ( obj1.in() );
        if ( CORBA::is_nil ( m_sm.in() ) )
        {
            FILE_LOG(logERROR)<<"IOR is not an SM object reference."<<endl;
        }
    }
    catch ( CORBA::COMM_FAILURE& ex )
    {
        FILE_LOG(logERROR)<<"Caught system exception COMM_FAILURE -- unable to contact the object."<<endl;
        throw DS_ServerConnectionException();
        return;
    }
    catch ( CORBA::SystemException& )
    {
        FILE_LOG(logERROR)<<"Caught a CORBA::SystemException."<<endl;
        throw DS_SystemException();
        return;
    }

    catch ( CORBA::Exception& )
    {
        FILE_LOG(logERROR)<<"Caught CORBA::Exception."<<endl;
        throw DS_Exception();
        return;
    }

    catch ( omniORB::fatalException& fe )
    {
        FILE_LOG(logERROR)<<"Cautght omniORB::fatalException:"<<endl;
        FILE_LOG(logERROR)<<" file: "<<fe.file() <<endl;
        FILE_LOG(logERROR)<<" line: "<<fe.line() <<endl;
        FILE_LOG(logERROR)<<" mesg: "<<fe.errmsg() <<endl;
        throw DS_FatalException();
        return;
    }
    catch ( ... )
    {
        FILE_LOG(logERROR)<<"Caught unknown exception."<<endl;
        throw DS_Exception();
        return;
    }
    return;




}


CClientSM::~CClientSM()
{

//
}

unsigned int CClientSM::callregisterSM(string objref)

{
    CORBA::Long id;
    //CORBA::string oref=CORBA::string_dup("test");

    id=m_sm->registerSM("test");

    FILE_LOG(logINFO)<<"Returned ID: "<<id<<endl;

    return id;
}
bool 	     CClientSM::callunregisterSM(unsigned int id)

{
    FILE_LOG(logINFO)<<"unregistering sm with id:"<<id<<endl;
    return m_sm->unregisterSM(id);
}
